package zy.wq.security.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import zy.wq.repository.SystemUserRepository;
import zy.wq.security.bean.SystemUser;
import zy.wq.security.bean.SystemUserQuery;
import zy.wq.security.service.SystemUserService;


@Service
public class SystemUserServiceImpl implements SystemUserService {

@Autowired private SystemUserRepository userRepository;
	
	@Autowired(required=false) private PasswordEncoder passwordEncoder;
	
	@Override
	public Page<SystemUser> findUsers(Pageable page, SystemUserQuery query) {
		return userRepository.findAll(page);
	}

	@Override
	public SystemUser getUserById(long id) {
		return userRepository.findById(id).orElse(null);
	}
	
	

	@Override
	public SystemUser getUserByName(String name) {
		return userRepository.getByName(name);
	}

	@Override
	public SystemUser addUser(SystemUser user) {
		if(StringUtils.hasText(user.getPassword()) && passwordEncoder != null) {
			user.setPassword(passwordEncoder.encode(user.getPassword()));
			System.out.println(user.getPassword());
		}
		return userRepository.save(user);
	}
	
}
